<template>
  <div>
    <el-form ref="form" :model="module" label-width="80px">
      <el-form-item label="权限姓名">
        <el-input v-model="module.roleName"  style="width: 217px;"></el-input>
      </el-form-item>

      <el-form-item label="权限">
        <el-input v-model="module.role"  style="width: 217px;"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onAdd">添加</el-button>
        <el-button @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import axios from "axios";

export default {
  name: "roleAddView",
  data(){       //data:{}
    return {
      module:{
        roleName:"",
        role:"",
      },

    }
  },

  methods:{
    onAdd(){
      //创建表格对象
      var formData = new FormData()
      for (const key in this.module){
        formData.append(key,this.module[key])
      }

      //请给我写一个确定添加的前置条件
      if(confirm("确定要添加吗")) {
        axios({
          url: "/api/module/add",
          method: "post",
          data: formData,
          //添加请求头
          // headers: {"Content-Type": "multipart/form-data"}
          headers:{"Authorization":"Bearer "+this.$store.state.user.token}
        }).then(res => {
          if(res.data.code==1){
            this.$message({
              message: '添加成功',
              type:'success'
            });
            //返回医生信息表
            this.$router.push("/module")
          }else {
            this.$message({
              message: res.data.message,
              type: 'error'
            });
          }

        })
      }

    },

    //删除文件
    removeImg(file){
      //根据uid删除对应的文件
      for (const key in this.fileList) {
        if (this.fileList[key].uid === file.uid) {
          this.fileList.splice(key, 1)
        }
      }
    },


    //添加图片
    changeImg(file){
      //直接将文件添加到数组中
      this.fileList.push(file)
      console.log("添加"+file.name)
    },
    cancel(){
      this.$router.push("/module")
    },
  }
}
</script>

<style scoped>

</style>
